<?php
/**
 * Created by PhpStorm.
 * User: zhanghua
 * Date: 2018/9/21
 * Time: 18:51
 */

namespace app\sun\controller;


use app\common\controller\SunBaseController;
use app\sun\service\AdminService;
use app\sun\service\VerifyService;
use app\sun\validate\LoginValidate;

class PublicController extends  SunBaseController
{

    /**
     * 系统后台登录展示页面
     * @return mixed
     */
    public function login()
    {

        if($this->request->isPost()){

            $loginValidate = new LoginValidate();

            $post = $this->request->post();

            $validateResult = $loginValidate->check($post);

            if(!$validateResult){
               $this->error($loginValidate->getError());
            }

            if (!VerifyService::check($post['verify'])) {
                $this->error('Please fill in the verification code correctly!');
            }


            $username = $post['username'];
            $password = $post['password'];

            $admin = AdminService::login($username, $password);

            if($admin){
                return redirect('/sun/index/index');
            }else{
                $this->error('账号密码有误');
            }

        }

        return $this->fetch();
    }

    /**
     * 系统退出
     */
    public function logout()
    {
        AdminService::logout();

        $this->redirect('/sun/public/login');
    }

    /**
     * 通用验证码
     */
    public function verify()
    {
        return VerifyService::entry();
    }

}